package com.xhj.util;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Api(value = "返回结果实体类")
public class ResultEntity {
    // 状态码
    public static final Integer SUCCESS_CODE = 20000;
    public static final Integer FAILED_CODE = 20001;

    public static final String SUCCESS = "SUCCESS";
    public static final String FAILED = "FAILED";

    @ApiModelProperty(value = "是否成功")
    private String result;
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data = new HashMap<>();

    public static  ResultEntity successWithoutData(){
        return new ResultEntity(SUCCESS,SUCCESS_CODE,null,null);
    }

    public static  ResultEntity successWithData(){
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setResult(SUCCESS);
        resultEntity.setMessage(null);
        resultEntity.setCode(SUCCESS_CODE);

        return resultEntity;
    }

    public ResultEntity setCode(Integer code) {
        this.code = code;
        return this;
    }

    public static  ResultEntity failedWithMessage(String message){
        return new ResultEntity(FAILED,FAILED_CODE,message,null);
    }

    public ResultEntity data(String key,Object value){
        this.data.put(key, value);
        return this;
    }

    public ResultEntity data(Map<String, Object> data){
        this.setData(data);
        return this;
    }
}
